<template>
  <div></div>
</template>

<script>
import dayjs from "dayjs";
import { AREA_MAP } from "@js/params";
export default {
  data() {
    return {
      tableRef: "tableRef",
      dataSource: [],
      form: {},
      modifyForm: {},
      total: 0,
      tableEvent: {
        action: this.handleAction,
        fetchTable: this.fetchTable,
      },
      loading: false,
      apiName: "warehouseWarehouseList",
      formKey: this.$quinn.guid(),
      tableKey: this.$quinn.guid(),
    };
  },
  computed: {
    expFileName() {
      return "文件导出";
    },
    columns() {
      const name = this.$options.name;
      return this.$columns[name] || [];
    },
    tableOpts() {
      const {
        tableRef = "tableRef",
        tableKey,
        formKey,
        form,
        dataSource,
        columns,
        total,
        loading,
      } = this;
      return {
        loading,
        ref: tableRef,
        key: tableKey,
        formKey,
        form,
        total,
        dataSource,
        columns,
      };
    },
  },
  watch: {
   
  },
  methods: {
    setModifyValue(row = {}, modify = "add") {
      for (const key in this.modifyForm) {
        let defValue = "";
        if (Array.isArray(this.modifyForm[key].value)) defValue = [];
        if (Object.hasOwnProperty.call(row, key))
          this.modifyForm[key].value = row[key];
        else this.modifyForm[key].value = defValue;
      }
    },
    formatQuery(opts) {
      const { date = [], ...params } = opts;
      if (date && date[0] && date[1]) {
        // 老系统时间参数 为startDate
        // params.startDate = dayjs(date[0]).format("YYYY-MM-DD");
        // params.endDate = dayjs(date[1]).format("YYYY-MM-DD");
        params.startTime = dayjs(date[0]).format("YYYY-MM-DD");
        params.endTime= dayjs(date[1]).format("YYYY-MM-DD");

      }
      return params;
    },
    formatData(res) {
      this.total = res?.count || res?.total || 0;
      this.dataSource = (res?.rows || res?.data || res || []).map((e) => {
        if (e.updatedAt)
          e.updatedTime = dayjs(e.updatedAt).format("YYYY-MM-DD HH:mm:ss");
        if (e.createdAt) {
          e.createdTime = dayjs(e.createdAt).format("YYYY-MM-DD HH:mm:ss");
        }
        return e;
      });
    },
    expTable(getAll = false) {
      let { dataSource: data, columns, expFileName: fileName } = this;
      this.$quinn.expTable({
        fileName,
        data: data.map((e, i) => {
          e.sortIndex = i + 1;
          return e;
        }),
        columns,
      });
    },
    formArea() {
      const items = [];
      for (const key in AREA_MAP) {
        if (Object.hasOwnProperty.call(AREA_MAP, key)) {
          const value = AREA_MAP[key];
          items.push({
            label: value,
            value: key,
          });
        }
      }
      return {
        tag: "el-select",
        label: "区域",
        value: "",
        items,
      };
    },
    onModifySubmit(e) {
      console.log(e);
    },
    formDirectoryId() {
      return {
        tag: "el-select",
        label: "套件",
        value: "",
        items: this.getDirectoryItem(),
        props: {
          clearable: true,
          filterable: true,
        },
      };
    },
    getDirectoryItem() {
      const selectMap = this.$store.state.directoryMap;
      return Object.keys(selectMap).map((value) => ({
        value,
        label: selectMap[value],
      }));
    },
    fetchCurrentTable() {
      const { tableRef } = this;
      const ref = this.$refs[tableRef];
      ref && ref.fetchTable();
    },
    handleAction(opts, row) {},
    formatProduct(res) {
      this.total = res?.total || 0;
      this.dataSource = (res?.data || []).map((e) => {
        if (e.updatedAt)
          e.updatedTime = dayjs(e.updatedAt).format("YYYY-MM-DD HH:mm:ss");
        if (e.createdAt) {
          e.createdTime = dayjs(e.createdAt).format("YYYY-MM-DD HH:mm:ss");
        }
        for (const k in e.directory || {}) {
          if (Object.hasOwnProperty.call(e.directory, k)) {
            e[`d_${k}`] = e.directory[k];
          }
        }
        e.directoryId = e.directory?.id;
        return e;
      });
    },
    fetchTable(opts) {
      const { apiName } = this;
      if (!apiName) return;
      this.loading = true;
      this.$api[apiName](this.formatQuery(opts))
        .then(this.formatData)
        .finally((e) => {
          this.loading = false;
        });
    },
    showDialog() {
      const ref = this.$refs.dialogRef;
      ref && ref.showDialog(...arguments);
    },
    showConfirm(text = "确认删除？", path = "", params = {}) {
      this.$confirm(text).then((res) => {
        this.$api[path](params).then((e) => {
          this.$message.success("操作成功");
          this.fetchCurrentTable();
        });
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
